set more off

capture erase tmp.dta
forvalues year = 2004(2)2016 {
	di "`year'"
	import excel using "turnout/TurnoutData`year'.xlsx", firstrow clear sheet("County VTO")
	if `year' == 2004 {
		rename H Turnout
		rename F TotalReg
		rename AX FIPS
	}
	if `year' == 2006 {
		rename H Turnout
		rename F TotalReg	
		rename AY FIPS
	}
	drop if mi(FIPS)	
	keep FIPS TotalReg Turnout A B
	gen year = `year'
	destring Turnout TotalReg, replace force
	rename TotalReg TotalReg
	if `year' > 2004 append using tmp
	save tmp, replace
}
erase tmp.dta

rename A countyname
rename B stateabbr
rename TotalReg totalregistered
rename Turnout totalvoting
rename FIPS fips
local myvars year fips totalregistered totalvoting
keep `myvars'
order `myvars'

duplicates drop fips year, force
reshape i fips
reshape j year
reshape xij totalregistered totalvoting
reshape wide

save "turnout/turnout", replace
